package com.spark.trouble.handle

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.helper.MutableStatePagerAdapter
import com.spark.trouble.HTInfoFragment
import com.spark.trouble.R
import com.spark.trouble.RectifyFragment
import com.spark.trouble.RejectOpinionFragment
import com.spark.trouble.model.TroubleDealListModel
import kotlinx.android.synthetic.main.act_handle.*
import me.yokeyword.fragmentation.SupportFragment

/**
 * [任务类型]
 * 1 -> 标准化检查 -> 主任务
 * 2 -> 标准化检查 -> 子任务
 * 3 -> 借阅申请
 * 4 -> 文件传输
 * 5 -> 归档申请
 * 6 -> 隐患 -> 主任务
 * 7 -> 隐患 -> 子任务(未分配)
 * 8 ->     -> 现场治理
 * 9 ->     -> 限时整改
 * 10 ->    -> 挂牌督办
 * 11 ->    -> 隐患升级
 * 12 ->    -> 重大隐患
 *
 * @info 处理流程状态
 *  1 -> 待处理
 *  2 -> 审核中/待审核
 *  3 -> 被驳回
 *  4 -> 确认中/待确认
 *  5 -> 整改中/待整改
 *  6 -> 验收中/待验收
 *  7 -> 验收驳回
 *  8 -> 待存储
 *  9 -> 已存储
 *  10 -> 已归档
 */
class HTHandleFramgnet : BaseFragment() {
    private lateinit var handleAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): HTHandleFramgnet {
            val handleFramgnet = HTHandleFramgnet()
            val bundle = Bundle()
            bundle.putString("data", data)
            handleFramgnet.arguments = bundle
            return handleFramgnet
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { activity?.finish() }
        arguments?.apply {
            getString("data")?.apply {
                val state = this
                Gson().fromJson(this, TroubleDealListModel.ResultMap::class.java)?.apply {
                    tv_title.text = stateFlag
                    val tabTitle = mutableListOf<String>()
                    val fragments = mutableListOf<Fragment>()
                    LogUtils.e("【task-type】$taskType 【state】${this.state} 【id】$task_id")
                    when (taskType) {
                    // 未分配任务
                        "7" -> {
                            tv_title.text = "处理方案"
                            tabTitle.apply {
                                add("现场治理")
                                add("限时整改")
                                add("挂牌督办")
                                add("重大隐患")
                            }
                            fragments.apply {
                                add(SpotHandleFragment.newInstance(Const.htTypeProcess, state))
                                add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                add(SupervisoryHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                add(MajorRiskFragment.newInstance(state))
                            }
                        }
                    // 现场治理
                        "8" -> {
                            when (this.state) {
                            // 审核中/待审核
                                "2" -> {
                                }
                            // 被驳回
                                "3" -> {
                                }
                            // 待存储
                                "4" -> {
                                }
                            }
                            tabTitle.apply {
                                add("现场治理")
                                add("现场情况")
                            }
                            fragments.apply {
                                add(SpotHandleFragment.newInstance(Const.htTypeProcess, state))
                                add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                            }
                        }
                    // 限时整改
                    // 挂牌督办
                        "9", "10" -> {
                            when (this.state) {
                            // 审核中，待审核
                            // 被驳回
                            // 确认中/待确认
                            // 整改中/待整改
                                "2", "3", "4", "5" -> {
                                    tabTitle.apply {
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    fragments.apply {
                                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                                    }
                                }
                            // 验收中/待验收
                                "6" -> {
                                    tabTitle.apply {
                                        add("整改情况")
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    fragments.apply {
                                        add(RectifyFragment.newInstance(task_id.toString()))
                                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                                    }
                                }
                            // 验收驳回
                                "7" -> {
                                    tv_title.text = "待整改"
                                    tabTitle.apply {
                                        add("驳回意见")
                                        add("整改通知书")
                                        add("现场情况")
                                    }
                                    fragments.apply {
                                        add(RejectOpinionFragment.newInstance())
                                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                                    }
                                }
                            // 待存储
                                "8" -> {
                                    tabTitle.apply {
                                        add("验收情况")
                                        add("整改情况")
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    fragments.apply {
                                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                                    }
                                }

                            }
                        }
                    // 隐患升级
                        "11" -> {
                            tabTitle.add("重大隐患")
                            tv_title.text = "重大隐患"
                            tabs.gone(true)
                            fragments.add(MajorRiskFragment.newInstance(state))
                        }
                    // 重大隐患
                        "12" -> {
                            // 2 审核中
                            // 3 被驳回
                            tabTitle.apply {
                                add("重大隐患")
                                add("现场情况")
                            }
                            fragments.apply {
                                add(MajorRiskFragment.newInstance(state))
                                add(HTInfoFragment.newInstance(Const.htTypeProcess, task_id.toString(), taskType))
                            }
                        }
                    }
                    // end of when
                    tabTitle.forEach {
                        tabs.addTab(tabs.newTab().setText(it))
                    }
                    handleAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
                        override fun getCount(): Int {
                            return tabTitle.size
                        }

                        override fun getPageTitle(position: Int): CharSequence? {
                            return tabTitle[position]
                        }
                    }
                    handleAdapter.adds(fragments)
                    pager.apply {
                        adapter = handleAdapter
                        canScroll = false
                    }
                    tabs.setupWithViewPager(pager)
                }
            }
        }
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        handleAdapter.pageList[pager.currentItem].fragment.let { it as SupportFragment }.onFragmentResult(requestCode, resultCode, data)
    }

}